﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;

namespace SL8.SL.TEST
{
    public partial class EventsInvocationTest : UserControl
    {
        public EventsInvocationTest()
        {
            InitializeComponent();
            this.btnButton.Click += new RoutedEventHandler(btnButton_Click);
        }

        void btnButton_Click(object sender, RoutedEventArgs e)
        {
            Delegate[] _List = GetObjectEventList(this.btnButton, "EventClick", typeof(Button));
        }

        public static Delegate[] GetObjectEventList(object p_Object, string p_EventName, Type p_EventType)
        {
            PropertyInfo[] ps = p_Object.GetType().GetProperties(); //(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Default);

           foreach (var item in ps)
           {
               System.Diagnostics.Debug.WriteLine(item.Name);
           }

            PropertyInfo _PropertyInfo = p_Object.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
            if (_PropertyInfo != null)
            {
                object _EventList = _PropertyInfo.GetValue(p_Object, null);
                //if (_EventList != null && _EventList is EventHandlerList)
                //{
                //    EventHandlerList _List = (EventHandlerList)_EventList;
                //    FieldInfo _FieldInfo = p_EventType.GetField(p_EventName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                //    if (_FieldInfo == null) return null;
                //    Delegate _ObjectDelegate = _List[_FieldInfo.GetValue(p_Object)];
                //    if (_ObjectDelegate == null) return null;
                //    return _ObjectDelegate.GetInvocationList();
                //}
            }
            return null;
        }
    }
}
